import datetime
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdrivermanager_cn import ChromeDriverManager

#driverIns=ChromeDriverManager().install()
#driver=webdriver.Chrome(service=Service(driverIns))
# #窗口设置大小
# #最大化
# driver.maximize_window()
# #最小化
# driver.minimize_window()
# #窗口全屏
# driver.fullscreen_window()
# #手动
# driver.set_window_size(1024,768)
# driver.get("https://www.baidu.com")
#xpath搜索元素
# text=driver.find_element(By.XPATH,'//*[@id="hotsearch-content-wrapper"]/li[2]/a/span[2]').text
# print(text)

#获取页面标题和URL
# title=driver.title
# url=driver.current_url
# print(title)
# print(url)
# # driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("nihao")
# driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title").click()


#窗口操作
# curWindow=driver.current_window_handle  #获取当前⻚⾯句柄
# allWindows=driver.window_handles      #获取所有⻚⾯句柄
#
# for window in allWindows:   #切换当前句柄为最新⻚⾯
#     if window != curWindow:
#         driver.switch_to.window(window)
# title=driver.title
# url=driver.current_url
# print(title)
# print(url)

#屏幕截图
##低阶版本
#driver.save_screenshot('images/image.png')
##进阶，每次生成图片不一样
# filename="autotest-"+datetime.datetime.now().strftime('%y-%m-%d-%H%M%S')+'.png'
# driver.save_screenshot('images/'+filename)
#
# driver.close()
# driver.quit()

#弹窗
#警告弹窗
# driver.get("file:///E:/测试/auto-test/selenium-html/alert.html")
# driver.find_element(By.CSS_SELECTOR,"#tooltip").click()
# alert=driver.switch_to.alert
#确认弹窗
# driver.get("file:///E:/测试/auto-test/selenium-html/confirm.html")
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# alert=driver.switch_to.alert
#提示弹窗
# driver.get("file:///E:/测试/auto-test/selenium-html/Prompt.html")
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# alert=driver.switch_to.alert
# alert.send_keys("123")
# #确认
# alert.accept()
# #取消
# #alert.dismiss()
# time.sleep(2)


##等待
# driver.get("https://www.baidu.com")
#强制等待
# time.sleep(5)
#隐式等待
# driver.implicitly_wait(5)
#显式等待
# wait=WebDriverWait(driver,2)
# wait.until(EC.invisibility_of_element((By.XPATH,'//*[@id="2"]/div/div/div[3]/div[1]/div[1]/div')))
#不要混合隐式和显式等待，可能会导致不可预测的等待时间。

#浏览器导航
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("https://tool.lu/")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)
# # driver.get("https://tool.lu/")
# driver.back()   #后退
# time.sleep(2)
# driver.forward()    #前进
# time.sleep(2)
# driver.refresh()    #刷新
# time.sleep(2)

#文件上传
# driver.get("file:///E:测试/auto-test/selenium-html/upload.html")
# ele=driver.find_element(By.CSS_SELECTOR,"body > div > div > input[type=file]")
# ele.send_keys("C:/Users/DELL/Desktop/防火墙实训.txt")   #直接发送文件路径
# time.sleep(2)

#1.打开浏览器----驱动管理
ChromeIns=ChromeDriverManager().install()
#浏览器参数配置
options = webdriver.ChromeOptions()
#添加无头模式,无界面显示
options.add_argument("-headless")

#添加页面加载策略
# options.page_load_strategy = 'common' #等待所有的资源加载完成
# options.page_load_strategy = 'eager'  #DOM访问就绪
options.page_load_strategy = 'none'     #完全不阻塞，直接继续往下执行脚本

###创建谷歌浏览器驱动对象
driver=webdriver.Chrome(service=Service(ChromeIns),options=options)
driver.get("https://haokan.baidu.com/")
print(driver.title)
driver.quit()
# driver.get("https://www.baidu.com/")
# driver.get("https://image.baidu.com/")
# time.sleep(3)

# ret=driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# for i in ret:
#     print(i.text)#获取每个元素对应的文本信息

